package com.qidian.mes.md.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.merge.AbstractMergeStrategy;
import com.qidian.mes.md.dict.WorkStationInfo;
import com.qidian.mes.md.service.MdDeptResourceService;
import com.qidian.mes.md.service.MdToolResourceService;
import com.qidian.mes.md.service.MdToolService;
import com.qidian.mes.md.service.MdWorkService;
import com.qidian.mes.md.vo.MdDeptResourceVO;
import com.qidian.mes.md.vo.MdToolResourceVO;
import com.qidian.mes.md.vo.MdToolVO;
import com.qidian.mes.pojo.MdWork;
import com.qidian.mes.util.pojo.QdResult;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: CoolPro
 * @Description: 文件下载(导出)
 * @Time: 2024-04-12 18:48
 **/

@RestController
@RequestMapping("/download")
@CrossOrigin
public class DownLoadController {
    @Autowired
    MdWorkService mdWorkService;

    @Autowired
    MdToolService mdToolService;

    @Autowired
    MdToolResourceService mdToolResourceService;

    @Autowired
    MdDeptResourceService mdDeptResourceService;

    @GetMapping("/work/allInfo")
    public void downloadAllWorkInfo(HttpServletResponse response) throws IOException {
        //导出所有数据
        QdResult<List<WorkStationInfo>> allInfo = mdWorkService.getAllInfo();
        List<WorkStationInfo> resData = new ArrayList<>();
        List<WorkStationInfo> data = allInfo.getData();
        if (data.size() > 0) {
            data.forEach(item -> {
                //获取设备列表
                List<MdToolVO> mdToos = (List<MdToolVO>) mdToolService.getMdToolsByWorkId(item.getIdWork()).getData();
                List<MdToolResourceVO> mdToolResource = (List<MdToolResourceVO>) mdToolResourceService.getToolResources(item.getIdWork()).getData();
                List<MdDeptResourceVO> dept = (List<MdDeptResourceVO>) mdDeptResourceService.getDeptResources(item.getIdWork()).getData();
                if (mdToos.size() > 0) {
                    mdToos.forEach(md -> {
                        item.setEquipments(md.getToolTypeName());
                        WorkStationInfo workStationInfo = new WorkStationInfo(
                                item.getIdWork(),
                                item.getWorkStationName(),
                                item.getWorkStationCode(),
                                item.getAddress(),
                                item.getStatus(),
                                item.getWorkshop(),
                                item.getProcess(),
                                item.getRemark()
                        );
                        workStationInfo.setEquipments(md.getToolTypeName());
                        resData.add(workStationInfo);
                    });
                }
                if (mdToolResource.size() > 0) {
                    mdToolResource.forEach(md -> {
                        WorkStationInfo workStationInfo = new WorkStationInfo(
                                item.getIdWork(),
                                item.getWorkStationName(),
                                item.getWorkStationCode(),
                                item.getAddress(),
                                item.getStatus(),
                                item.getWorkshop(),
                                item.getProcess(),
                                item.getRemark()
                        );
                        workStationInfo.setFixtures(md.getToolName());
                        resData.add(workStationInfo);
                    });
                }
                if (dept.size() > 0) {
                    dept.forEach(md -> {
                        WorkStationInfo workStationInfo = new WorkStationInfo(
                                item.getIdWork(),
                                item.getWorkStationName(),
                                item.getWorkStationCode(),
                                item.getAddress(),
                                item.getStatus(),
                                item.getWorkshop(),
                                item.getProcess(),
                                item.getRemark()
                        );
                        workStationInfo.setHumanResources(md.getDeptName());
                        resData.add(workStationInfo);
                    });
                }
            });
        }
        //导出  固定写法application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
        try {
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("工作站-" + System.currentTimeMillis(), "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), WorkStationInfo.class)
                    .sheet("工作站信息")
                    .doWrite(resData);
        } catch (IOException e) {
            response.reset();//清空输出流
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            response.getWriter().println(QdResult.error("文件下载失败"));
        }
    }
}
